package messages.persondispatcher;

import java.io.IOException;

import state.person.Person;
import state.person.events.PersonRegisterEvent;
import messages.BaseMessage;
import messages.ByteList;
import messages.MessageType;

public class PersonCreationMessage extends BaseMessage {
	
    private Person p;
    
	public PersonCreationMessage(byte[] data)
	{
		super(data);

		PersonRegisterEvent event = new PersonRegisterEvent(data, 1);
		
		p = event.getPerson();
	}
	
	public PersonCreationMessage(Person person) throws IOException
	{
		super(null);
		
		p = person;
	}

	@Override
	public byte[] toByteArray()
	{
	    ByteList list = new ByteList();
	    list.addByte(getMessageType().getIdentifier());
	    
	    PersonRegisterEvent event = new PersonRegisterEvent(p);
	    
	    event.appendToByteList(list);
	    
		return list.toByteArray();
	}

	@Override
	public MessageType getMessageType()
	{
		return MessageType.PersonCreationMessage;
	}
	
	public Person getPerson()
	{
        return p;
	}

    @Override
    public int getMessageSize()
    {
        return -1;
    }

}
